/*
 * FrmRelatorioFatura.java
 *
 * Created on 26 de Novembro de 2009, 09:56
 */

package br.com.pangalacteos.apresentacao;

import br.com.pangalacteos.dao.FaturaDAO;
import br.com.pangalacteos.modelo.Fatura;
import br.com.pangalacteos.modelo.Fornecedor;
import br.com.pangalacteos.util.Parser;
import br.com.pangalacteos.util.Validator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  rafali
 */
public class FrmRelatorioFatura extends javax.swing.JPanel {

    /** Creates new form FrmRelatorioFatura */
    public FrmRelatorioFatura() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chkAbertas = new javax.swing.JCheckBox();
        chkPagas = new javax.swing.JCheckBox();
        chkEstornadas = new javax.swing.JCheckBox();
        btnExecutar = new javax.swing.JButton();
        txtLancamentoDe = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtFornecedor = new javax.swing.JTextField();
        txtLancamentoAte = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtVencimentoDe = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        txtVencimentoAte = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        txtPagamentoDe = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        txtPagamentoAte = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultado = new javax.swing.JTable();
        btnCancelar = new javax.swing.JButton();

        chkAbertas.setText("Faturas Abertas");

        chkPagas.setText("Faturas Pagas");

        chkEstornadas.setText("Faturas Estornadas");

        btnExecutar.setText("Executar");
        btnExecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExecutarActionPerformed(evt);
            }
        });

        try {
            txtLancamentoDe.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel1.setText("Lançamento de:");

        jLabel3.setText("Fornecedor:");

        try {
            txtLancamentoAte.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel2.setText("Lançamento até:");

        jLabel4.setText("Vencimento de:");

        try {
            txtVencimentoDe.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel5.setText("Vencimento até:");

        try {
            txtVencimentoAte.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel6.setText("Data Pagamento de:");

        try {
            txtPagamentoDe.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel7.setText("Data Pagamento até:");

        try {
            txtPagamentoAte.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        tblResultado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Fornecedor", "Dt. Fatura", "Vencimento", "Pagamento", "Montante", "Status", "Referência"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblResultado);

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(chkPagas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(chkAbertas, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(chkEstornadas, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPagamentoDe)
                    .addComponent(txtVencimentoAte)
                    .addComponent(txtVencimentoDe)
                    .addComponent(txtLancamentoAte)
                    .addComponent(txtLancamentoDe)
                    .addComponent(txtFornecedor)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(txtPagamentoAte)
                    .addComponent(btnExecutar, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(1, 1, 1)
                        .addComponent(txtFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(jLabel1)
                        .addGap(1, 1, 1)
                        .addComponent(txtLancamentoDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addGap(1, 1, 1)
                        .addComponent(txtLancamentoAte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addGap(1, 1, 1)
                        .addComponent(txtVencimentoDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addGap(1, 1, 1)
                        .addComponent(txtVencimentoAte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addGap(1, 1, 1)
                        .addComponent(txtPagamentoDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addGap(1, 1, 1)
                        .addComponent(txtPagamentoAte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chkAbertas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkPagas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkEstornadas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExecutar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnExecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExecutarActionPerformed
    try {
        validaCampos();
        //prepara parâmetros
        Fornecedor fornecedor = Parser.parseFornecedor(txtFornecedor.getText());
        Date lancamentoDe = Parser.parseDateNullable(txtLancamentoDe.getText());
        Date lancamentoAte = Parser.parseDateNullable(txtLancamentoAte.getText());
        Date vencimentoDe = Parser.parseDateNullable(txtVencimentoDe.getText());
        Date vencimentoAte = Parser.parseDateNullable(txtVencimentoAte.getText());
        Date pagamentoDe = Parser.parseDateNullable(txtPagamentoDe.getText());
        Date pagamentoAte = Parser.parseDateNullable(txtPagamentoAte.getText());
        
        List<Fatura> resultado = new FaturaDAO().generateRelatorioFatura(
                fornecedor, 
                lancamentoDe, lancamentoAte, 
                vencimentoDe, vencimentoAte, 
                pagamentoDe, pagamentoAte,
                chkAbertas.isSelected(),
                chkPagas.isSelected(),
                chkEstornadas.isSelected());
        
        DefaultTableModel modelo = (DefaultTableModel)tblResultado.getModel();
        modelo.setRowCount(0);
        
        Iterator<Fatura> it = resultado.iterator();
        
        while(it.hasNext()){
            Fatura corrente = it.next();
            modelo.addRow(new Object[]{"", "", "", "","", "", "", ""});
            int ultimaLinha = tblResultado.getRowCount() - 1;
            tblResultado.setValueAt(corrente.getCdfatura(), ultimaLinha, 0);
            tblResultado.setValueAt(corrente.getCdfornecedor().getNmfornecedor(), ultimaLinha, 1);
            tblResultado.setValueAt(Parser.formatDateNullable(corrente.getDtfatura()), ultimaLinha, 2);
            tblResultado.setValueAt(Parser.formatDateNullable(corrente.getDtvencimento()), ultimaLinha, 3);
            tblResultado.setValueAt(Parser.formatDateNullable(corrente.getDtpagamento()), ultimaLinha, 4);
            tblResultado.setValueAt(corrente.getVlmontante(), ultimaLinha, 5);
            tblResultado.setValueAt(corrente.getCdstatus(), ultimaLinha, 6);
            tblResultado.setValueAt(corrente.getCdreferencia(), ultimaLinha, 7);
        }
        
    } catch (Exception ex){
        JOptionPane.showMessageDialog(null, "Campo " + ex.getMessage() + ", por favor revise", "Erro",
             JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
}//GEN-LAST:event_btnExecutarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    FrmPrincipal.fecharAba(this, false);
}//GEN-LAST:event_btnCancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExecutar;
    private javax.swing.JCheckBox chkAbertas;
    private javax.swing.JCheckBox chkEstornadas;
    private javax.swing.JCheckBox chkPagas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblResultado;
    private javax.swing.JTextField txtFornecedor;
    private javax.swing.JFormattedTextField txtLancamentoAte;
    private javax.swing.JFormattedTextField txtLancamentoDe;
    private javax.swing.JFormattedTextField txtPagamentoAte;
    private javax.swing.JFormattedTextField txtPagamentoDe;
    private javax.swing.JFormattedTextField txtVencimentoAte;
    private javax.swing.JFormattedTextField txtVencimentoDe;
    // End of variables declaration//GEN-END:variables
    
    private void validaCampos() throws Exception {
        Validator.existeIdEmBranco(Fornecedor.class, Parser.parseIntNullable(txtFornecedor.getText()), "Fornecedor");
        Validator.dataEmBranco(txtLancamentoDe.getText(), "Lançamento de");
        Validator.dataEmBranco(txtLancamentoAte.getText(), "Lançamento até");
        Validator.dataEmBranco(txtVencimentoDe.getText(), "Vencimento de");
        Validator.dataEmBranco(txtVencimentoAte.getText(), "Vencimento até");
        Validator.dataEmBranco(txtPagamentoDe.getText(), "Pagamento de");
        Validator.dataEmBranco(txtPagamentoAte.getText(), "Pagamento até");
    }
}
